package com.czd.support.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.czd.base.utils.WebUtils;
import com.czd.support.model.Company;
import com.czd.support.model.Manager;
import com.czd.support.service.CompanyService;

/**
 * @Description 企业信息导航
 * @Author JinMa.Xiao
 * @Date 2015年6月18日 下午4:56:00
 * @Version v1.0
 */
@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController {

	@Autowired
	private CompanyService companyService;

	@RequestMapping(value = { "", "/", "/list" })
	private String list(HttpServletRequest request, Model model,
			Company company,
			@RequestParam(value = "pageNo", defaultValue = "1") int pageNo) {
		company.setPageNo(pageNo);
		String kw = WebUtils.findParamStr(request, "kw");
		model.addAttribute("paginate",
				companyService.queryForPaginate(company, kw));
		return "company/list";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add() {
		return "company/add";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(HttpServletRequest httpServletRequest, Company company) {
		Manager sesionUser = this.getSesionUser(httpServletRequest);
		company.setCreateUser(sesionUser.getCreateUser());
		if (companyService.insert(company)) {
			return "redirect:/company/list";
		} else {
			return "/login";
		}
	}

	@RequestMapping(value = "/{id}/details", method = RequestMethod.GET)
	public String details(@PathVariable int id, Model model) {
		Company company = companyService.findById(id);
		model.addAttribute("company", company);
		return "company/details";
	}

	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String delete(@PathVariable int id) {
		companyService.deleteById(id);
		return "redirect:/company/list";
	}

	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	public String edit(@PathVariable int id, Model model) {
		Company company = companyService.findById(id);
		model.addAttribute("company", company);
		return "company/edit";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String edit(HttpServletRequest request, Model model, Company company) {
		Manager sesionUser = this.getSesionUser(request);
		company.setUpdateUser(sesionUser.getUpdateUser() );
		companyService.update(company);
		model.addAttribute("company", company);
		return "company/details";
	}

}
